
package com.scott.crm.user.controller;

import com.scott.base.BaseBean;
import com.scott.base.message.DataRes;
import com.scott.base.message.DataUtil;
import com.scott.base.utils.AESUtil;
import com.scott.crm.user.bean.UserBean;
import com.scott.crm.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


/**
 * @author 黄后林
 * @version 1.0
 * @note
 * @date 2021-11-27 14:24:01
 */
@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService service;




    @RequestMapping(value = "login", method = RequestMethod.POST)
    public DataRes login(@RequestBody UserBean user) throws Exception {

        UserBean userBean = service.login(user);


        if (userBean == null) {
            return DataUtil.errors();
        } else if(userBean.getId() == 1){
            return DataUtil.accessFailed();
        }else {

            // 从数据库查询中拿到userId
            Integer id = userBean.getId();
            String userId = String.valueOf(id);

            //加密userId拿到token
            String token = AESUtil.encrypt(userId, AESUtil.KEY);
            userBean.setToken(token);

            // 防止前端拿到userId
            userBean.setId(-1);

            // 把token返回给前端 并且每次请求必须带这个token 否则找不到userId
            return DataUtil.success(userBean);
        }
    }

    /**
     * 方法描述:http://localhost:8080/user/updatePassword
     *
     * @author: 周海燕
     * @date: 2021/12/1 16:31 [user]
     */
    @RequestMapping(value = "updatePassword", method = RequestMethod.POST)
    public DataRes updatePassword(@RequestBody UserBean user) throws Exception {
        // 拿token
        String token = user.getToken();

        // 拿到token就要解密才能拿到userId
        // decrypt解密的方法
        //         第一个参数是token，也就是加密后的userId的公钥
        //         第二个参数是AESUtil.KEY定义的私钥
        String decrypt = AESUtil.decrypt(token, AESUtil.KEY);

        user.setId(Integer.parseInt(decrypt));


        Integer row = service.updatePassword(user);

        if (row == null) {
            return DataUtil.errors();
        } else {
            return DataUtil.success(null);
        }
    }


    /**
     * 方法描述:http://localhost:8080/user/loginLoading
     *
     * @author: 周海燕
     * @date: 2021/12/1 16:32 [user]
     */
    @RequestMapping(value = "loginLoading", method = RequestMethod.POST)
    public DataRes loginLoading(@RequestBody UserBean user) throws Exception {
        // 拿token
        String token = user.getToken();

        // 拿到token就要解密才能拿到userId
        // decrypt解密的方法
        //         第一个参数是token，也就是加密后的userId的公钥
        //         第二个参数是AESUtil.KEY定义的私钥
        String decrypt = AESUtil.decrypt(token, AESUtil.KEY);

        user.setId(Integer.parseInt(decrypt));

        UserBean userBean = service.loginLoading(user);

        if (userBean == null) {
            return DataUtil.errors();
        } else {
            return DataUtil.success(userBean);
        }
    }

    /**
     * 方法描述:http://localhost:8080/user/register
     *
     * @author: 周海燕
     * @date: 2021/11/25 14:57 [userBean] java.util.Map<java.lang.String,java.lang.Object>
     * @CrossOrigin；解决跨域
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public DataRes register(@RequestBody UserBean user) throws Exception {


        // 是否插入成功
        Integer row = service.register(user);

        // 拿到用户的phone返回给前端
        String phone = user.getPhone();

        if (row == 1) {
            return DataUtil.success(phone);
        } else {
            return DataUtil.registrationFailed();
        }

    }






}

